﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using DTO;

namespace DAO
{
    public class DAO_PhieuMuon
    {
        public Boolean ThemPhieuMuon(DTO_PhieuMuon ttPhieuMuon){
            string query = "insert into PhieuMuon(MaPhieuMuon,MaDocGia,NgayMuon,MaNV) values('"+ttPhieuMuon.MaPhieuMuon+"','"+ttPhieuMuon.MaDocGia+"','"+ttPhieuMuon.NgayMuon+"',"+ttPhieuMuon.MaNV+")";
            if (DataProvider.Capnhatdl(query) > 0) {
                return true;
            }
            return false;
        }

        public string TaoPhieuMuon()
        {
            string MaPM = "";
            string NgayHH = System.DateTime.Now.ToShortDateString();
            int SoNgay = DemSoPhieuMuonTheoNgay(NgayHH);
            MaPM = "PM" + NgayHH + SoNgay;
            return MaPM;
        }

        private int DemSoPhieuMuonTheoNgay(string Ngay)
        {
            int SoDong = 0;
            string query = "select count(maphieumuon) from PhieuMuon where maphieumuon like '%" + Ngay + "%'";
            DataTable dtSoDong = new DataTable();
            dtSoDong = DataProvider.laydulieubang(query,DataProvider.c);
            SoDong = int.Parse(dtSoDong.Rows[0][0].ToString());
            if(DataProvider.c.State==ConnectionState.Open) DataProvider.c.Close();
            return SoDong + 1;
        }

        public DataTable DanhSachMuon(string p)
        {
            string query = "SELECT ChiTietPhieuMuon.* FROM PhieuMuon INNER JOIN ChiTietPhieuMuon ON PhieuMuon.MaPhieuMuon = ChiTietPhieuMuon.MaPhieuMuon where PhieuMuon.madocgia="+p;
            return DataProvider.laydulieubang(query, DataProvider.c);
        }

       
    }
}
